<?php
namespace Home\Controller;
use Think\Controller;
class EvaluateController extends CommonController {


    /**
     * 添加评论
     */
	public function addevaluate(){
		$uid = I('post.uid',0,'intval');
		$content = I('post.content');
		$goodsid = I('post.goodsid',0,'intval');
        $orderid = I('post.orderid');
		if(!$uid)  returnApiError(1,'用户id参数错误');
		if(!$content)  returnApiError(1,'评论内容未填写');
		if(!$goodsid)  returnApiError(1,'商品id参数错误');
        if(!$orderid)  returnApiError(1,'orderid参数错误');

        $order = M('order');
		$order_goods = M('order_goods');
		$member = M('member');
		$evaluate = M('evaluate_goods');

		$order_where['order_id'] = $orderid;
		$order_goods_result = $order_goods->where($order_where)->find();
		if(!$order_goods_result) returnApiError(1,'数据不存在');

        //查询是否评价

        $evaluate_where['geval_orderid'] = $orderid;
        $evaluate_where['geval_goodsid'] = $goodsid;

        $evaluate_data = $evaluate->where($evaluate_where)->select();

        if($evaluate_data){

            foreach ($evaluate_data as $k=>$v) {
                foreach ($v as $k2=>$v2) {
                    if($v2 == null){
                        //echo $k2;
                        $evaluate_data[$k][$k2] = "";
                    }
                }

            }

            returnApiSuccess('已经评论过了',$evaluate_data);
        }

        $member_result = $member->where("member_id = $uid")->find();

        $order_data = $order->find($orderid);

        if($order_data['order_state'] != 40){
            returnApiError(1,'订单未完成，不能评价,订单code='.$order_data['order_state']);
        }

        $store = M('store')->find($order_goods_result['store_id']);

        $data['geval_orderid'] = $orderid;
        $data['geval_orderno'] = $order_data['order_sn'];
        $data['geval_ordergoodsid'] = $order_goods_result['rec_id'];
        $data['geval_goodsid'] = $goodsid;
        $data['geval_goodsname'] = $order_goods_result['goods_name'];
        $data['geval_goodsprice'] = $order_goods_result['goods_price'];
        $data['geval_goodsimage'] = $order_goods_result['goods_image'];
        $data['geval_storeid'] = $order_goods_result['store_id'];
        $data['geval_scores'] = 5;
        $data['geval_storename'] = $store['store_name'];
        $data['geval_content_again'] = " ";
        $data['geval_content'] = urlencode($content);
        $data['geval_isanonymous'] = " ";
        $data['geval_addtime'] = time();
        $data['geval_addtime_again'] = " ";
        $data['geval_explain_again'] = " ";
        $data['geval_image_again'] = " ";
        $data['geval_storeid'] = $order_goods_result['store_id'];
        $data['geval_frommemberid'] = $uid;
        $data['geval_frommembername'] = $member_result['member_name'];


        if($evaluate->add($data)){
            $goods_save['evaluation_state'] = 1;
            $goods_map['order_id'] = $orderid;
            $goods_map['goods_id'] = $goodsid;
            $order_goods->where($goods_map)->save($goods_save);
            $evaluate_data = $evaluate->where($evaluate_where)->select();
            foreach ($evaluate_data as $k=>$v) {
                foreach ($v as $k2=>$v2) {
                    if($v2 == null){
                        //echo $k2;
                        $evaluate_data[$k][$k2] = "";
                    }
                }

            }

            //判断是否所有商品都评论过
            $goods_where['buyer_id'] = $uid;
            $goods_where['order_id'] = $orderid;
            $goods_where['evaluation_state'] = 0;
            $order_goods_data = $order_goods->where($goods_where)->select();


            if(!$order_goods_data){
                $order_save['evaluation_state'] = 1;
                $order->where("order_id = $orderid")->save($order_save);
            }
            //评论赠积分
            $pointlog = D('PointsLog');  
            $insertarr["pl_memberid"] =$member_result['member_id'];
            $insertarr["pl_membername"] =$member_result['member_name'];
            $result = $pointlog->insert("comments",$insertarr);
            returnApiSuccess('ok');
           // returnApiSuccess('评论成功',$evaluate_data);
        }
        returnApiError(1,'评论失败');
	}



    /**
     * 评论列表
     */
    public function lists(){
        $orderid = I('orderid');
        $uid = I('uid');
        if(!$orderid) returnApiError(1,'orderid参数错误');
        if(!$uid) returnApiError(1,'uid参数错误');

        $order_goods = M('OrderGoods');
        $evaluate = M('EvaluateGoods');

        //实例化浏览历史
        $where['order_id'] = $orderid;
        $goods_data = $order_goods->field('goods_id')->where($where)->select();
        if(!$goods_data) returnApiSuccess('数据为空');
        foreach($goods_data as  $k=>$v){
            $evaluate_where['geval_orderid'] = $orderid ;
            $evaluate_where['geval_goodsid'] = $v['goods_id'] ;
            $evaluate_where['geval_frommemberid'] = $uid;
            $evaluate_data = $evaluate->field('geval_id,geval_content')->where($evaluate_where)->find();
            if(!$evaluate_data){
                $goods_data[$k]['evaluate_state'] = 0;
                $goods_data[$k]['evaluate_list'] = '';
            }else{
                $goods_data[$k]['evaluate_state'] = 1;
                $goods_data[$k]['evaluate_list'] = urldecode($evaluate_data['geval_content']);
            }
        }

        returnApiSuccess('ok',$goods_data);
    }




}